<?

if (!$user)
{
  include "page_personal_logon.php";
  return;
}

$PageTitle=$msg["Page_ChangeProfile"];

include "div_menu_personal.php";


if (!$_POST) 
{
  make_form($user);
}
else
{
  $new_user = array(
    "id"=>$user["id"],
    "name"=>trim(stripslashes($_POST["name"])), 
    "email"=>trim(stripslashes($_POST["email"])), 
    "fullname"=>stripslashes($_POST["fullname"]), 
    "comment"=>stripslashes($_POST["comment"]));

  if (save_user($new_user)) 
  {
    $_SESSION["user"] = db_read_user_by_id($user["id"]);

    print_continue($msg["Saved"], "personal");
  } 
  else 
  {
    make_form($new_user);
  };
};


//-----------------------------------------------------------------------------

function make_form($user) 
{
  global $msg;
?>

<!-- Edit user form -->

<div class="form_area">
<div class="form_div">
<form action="personal_change_profile" method="post">

  <fieldset class="fields1">

  <dl>
    <dt><label for="name"><?=$msg["UserName"];?></label></dt>
    <dd><input type="text" size="25" tabindex="1" name="name" id="name" value="<?=maskqoute($user["name"]);?>"/></dd>
  </dl>

  <dl>
    <dt><label for="email"><?=$msg["EMail"];?></label></dt>
    <dd><input type="text" size="25" tabindex="2" name="email" id="email" value="<?=maskqoute($user["email"]);?>"/></dd>
  </dl>

  <dl>
    <dt><label for="fullname"><?=$msg["FullName"];?></label></dt>
    <dd><input type="text" size="25" tabindex="3" name="fullname" id="fullname" value="<?=maskqoute($user["fullname"]);?>"/></dd>
  </dl>

  <dl>
    <dt><label for="comment"><?=$msg["Comment"];?></label></dt>
    <dd><textarea rows="5" cols="40" tabindex="4" name="comment" id="comment"/><?=$user["comment"];?></textarea></dd>
  </dl>

  <dl class="button_div">
    <button type="submit" tabindex="5" name="save"><?=$msg["Save"];?></button>
    &nbsp;
    <button type="button" tabindex="6" name="cancel" onClick="window.location.href='personal'"><?=$msg["Cancel"];?></button>
  </dl>

  </fieldset>

</form>
</div>
</div>

<?php
};


function save_user($user) 
{
  global $msg;

  $n = 0;

  try 
  {
    db_call();

    if ($user["name"] == "") 
    {
      $errors[$n++] = $msg["NoUserName"];
    } 
    else 
    {
      if (db_check_user("name", $user["name"], $user["id"]) > 0) 
      {
        $errors[$n++] = sprintf($msg["UserExists"], $user["name"]);
      }
    }

    if ($user["email"] == "") 
    {
      $errors[$n++] = $msg["NoEMail"];
    } 
    else 
    {
      if (db_check_user("email", $user["email"], $user["id"]) > 0) 
      {
        $errors[$n++] = sprintf($msg["EMailExists"], $user["email"]);
      }
    }


    if ($n == 0) 
    {
      db_update_user($user["id"], $user);
    }
  } 
  catch (Exception $e) 
  {
    $errors[$n++] = $e->getMessage();
  }

  if ($n <> 0) 
  {
    print_errors($errors);
    return False;
  }

  return $user;
};

?>
